import { onMounted, onUnmounted, ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { spanTypes } from "../utils/types";

export const useHookOne = () => {
    const spanList = reactive<Array<spanTypes>>([])
    const bgstar = ref()
    const router = useRouter()
    const getSpanListInit = () => {
        let width = bgstar.value && bgstar.value.clientWidth;
        let height = bgstar.value && bgstar.value.clientHeight;
        for (var i = 0; i < 520; i++) {
            let left = Math.floor(Math.random() * width)
            let top = Math.floor(Math.random() * height)
            let scaler = (Math.random() * 1.5)
            let rate = (Math.random() * 2.5)

            let r = Math.random() * 255;
            let g = Math.random() * 255;
            let b = Math.random() * 255;
            let opacity = Math.random() * 1
            spanList.push({
                left: left + 'px',
                top: top + 'px',
                transform: `scale(${scaler})`,
                animationDelay: rate + 's',
                background: `rgba(${r},${g},${b},${opacity})`,
            })
        }
    }
    const gotowhere = (options: string | any) => {
        router.push(options)
    }

    onMounted(() => {
        getSpanListInit()
    })

    function getWeekInYear() {
        var endDate: any = new Date(),
            curYear: any = endDate.getFullYear(),
            startDate: any = new Date(curYear, 0, 1);
        var startWeek = startDate.getDay(),
            offsetWeek = 0;
        if (startWeek != 1) {
            offsetWeek = 1;
            if (!startWeek) {
                startDate.setDate(1);
            } else {
                startDate.setDate(8 - startWeek);
            }
        }
        var distanceTimestamp = endDate - startDate,
            days = Math.ceil(distanceTimestamp / (24 * 60 * 60 * 1000)) + startWeek,
            weeks = Math.ceil(days / 7) + offsetWeek;
        return weeks;
    }

    return {
        spanList,
        bgstar,
        getSpanListInit,
        gotowhere,
        getWeekInYear
    }
}